
#include <assert.h>

#include "Logger.hpp"
#include "Socket.hpp"
#include "SocketsOps.hpp"
#include "InetAddress.hpp"

namespace tulun
{
    // class InetAddress;
    // class Socket::
    // const int sockfd_; // listenFd; RreaWriteFd;

    Socket::Socket(int sockfd)
        : sockfd_(sockfd)
    {
    }
    Socket::~Socket()
    {
        Sockets::close(sockfd_);
    }

    int Socket::fd() const
    {
        return sockfd_;
    }
    void Socket::bindAddress(const InetAddress &localAddr)
    {
        Sockets::bindOrDie(sockfd_, localAddr);
    }
    void Socket::listen()
    {
        Sockets::listenOrDie(sockfd_);
    }
    int Socket::accept(InetAddress *peerAddr)
    {
        assert(peerAddr != nullptr);
        struct sockaddr_in addr = {};
        int connfd = Sockets::accept(sockfd_, &addr);
        if (connfd > 0)
        {
            peerAddr->setSockAddrInet(addr);
        }
        return connfd;
    }
    void Socket::shutdownWrite()
    {
        Sockets::shutdownWrite(sockfd_);
    }
}